///////////////////////////////////////////////////////////////////
// Student name:Jon Hersack and Caleb Reinking
// Course: COSC 3403
// Assignment: #7 - Initial Software Construction
// File name:Team.java
// Purpose: Tracks team score and manages active players and
//          distributes cards to players
// Assumptions: constructor assumes correct input of team type
// Limitations:none known
// Development Computer:Intel core2 duo 2.67Ghz
// Operating System: windows
// Integrated Development Environment (IDE): netbeans 6.5
// Compiler: Javac: java 1.6.0_13
///////////////////////////////////////////////////////////////////

import java.util.*;

public class Team
{
    Vector<Player> playerVect;
    ScoreKeeper handScorer;
    int gameScore;
    int handMileage;
    int activePlayer;
    int teamId;
    boolean speedLimit;
    Vector<Pile> pileVect;//pile 0 = speed, 1 = battle
   // Boolean[] safetyArea = new Boolean[4];
    //order:ROW, DA, PP, ET
   // Boolean[] hazardStatus = new Boolean[5];
    //order: stop, speedLimit, out of gas, flattire, accident
    String[] name = new String[3];

    //##########################################

    Team(int teamType, String[] teamName, int id)
    {
        name = teamName;
        playerVect = new Vector<Player>(2);//two players per team

        if(teamType==1)//add a human player
        {
             playerVect.add(new HumanPlayer(true, id, teamName[1]));
           // playerVect.add(new HumanPlayer(true, id, "I'm not a machine"));
        }//end if
        else//add a computer player
        {
           // playerVect.add(new ComputerPlayer(false, id, "Machine 1 team"+id));
             playerVect.add(new ComputerPlayer(false, id, teamName[1]));
        }//end else

        playerVect.add(new ComputerPlayer(false, id, teamName[2]));
        //playerVect.add(new ComputerPlayer(false, id, "Machine 2 team"+id));//add second player
        teamId = id;

        gameScore = 0;
        handMileage = 0;
        activePlayer = 0;
        pileVect = new Vector<Pile>(2);
        pileVect.add(new Pile());
        pileVect.add(new Pile());
        handScorer = ScoreKeeper.getInstanceOf();
      
    }//end constructor

    //############################################
    Vector<Player> getPlayers()
    {
        return playerVect;
    }// end getPlayers

    //############################################
    void playOnPile(Card card)
    {
        Table myTable = Table.getInstanceOf();
        Boolean[] safetyArea = myTable.getSafetyArea(teamId);
        Boolean[] hazardStatus = myTable.getHazardStatus(teamId);
        //order: 0:stop, 1:speedLimit, 2:out of gas, 3:flattire, 4:accident        
        switch( card.getTypeID() )
        {
            case 1: addMiles(25);
                    break;
            case 2: addMiles(50);
                    break;
            case 3: addMiles(75);
                    break;
            case 4: addMiles(100);
                    break;
            case 5: addMiles(200);//will only get here if it is valid
                    myTable.twoHundredsLeft[teamId]--;
                    handScorer.incTwoHundredsPlayed(teamId);
                    break;
                //card goes to distance pile
               // pileVect.elementAt(0).playCard(card);
                //add to score
            case 6:
                  //pileVect.elementAt(1).playCard(card);
                  //resetHazards();
                  myTable.resetHazards(teamId);
                  hazardStatus[2]=true;
                  break;
            case 7:
                 // pileVect.elementAt(1).playCard(card);
                  //resetHazards();
                  myTable.resetHazards(teamId);
                  hazardStatus[3]=true;
                  break;
            case 8:
                  //pileVect.elementAt(1).playCard(card);
                  //resetHazards();
                  myTable.resetHazards(teamId);
                  hazardStatus[4]=true;
                  break;
            case 10: // card goes on other team's battle pile
                 // pileVect.elementAt(1).playCard(card);
                  //resetHazards();
                  myTable.resetHazards(teamId);//set stop to true and clear all others
                  break;
            case 9: // card goes on other team's speed pile
                //resetHazards();
                boolean oldRollstatus = hazardStatus[0];
                myTable.resetHazards(teamId);//set stop to true and clear all others
                hazardStatus[0]=oldRollstatus;//don't erase roll on speed lmit play
                hazardStatus[1]=true;
                //pileVect.elementAt(0).playCard(card);
                     break;
            case 11://gas
                //hazard order: stop, speedLimit, out of gas, flattire, accident
                hazardStatus[2]=false;
               // pileVect.elementAt(1).playCard(card);
                    break;
            case 12://tire
                hazardStatus[3]=false;
                //pileVect.elementAt(1).playCard(card);
                    break;
            case 13://repair
                hazardStatus[4]=false;
                //pileVect.elementAt(1).playCard(card);
                    break;
            case 15://roll
                hazardStatus[0]=false;
                //pileVect.elementAt(1).playCard(card);
                    break;
            case 14://limit
               // pileVect.elementAt(0).playCard(card);
                hazardStatus[1] = false;
                    break;
            // card goes on own team's safety pile
            case 16: safetyArea[3]=true;//et
            handScorer.incSafetyPlayed(teamId);
                    break;
            case 17: safetyArea[2]=true;//pp
            handScorer.incSafetyPlayed(teamId);
                    break;
            case 18: safetyArea[1]=true;//da
            handScorer.incSafetyPlayed(teamId);
                    break;
            case 19: safetyArea[0]=true;//row
            handScorer.incSafetyPlayed(teamId);
                    break;
        }//end switch
        myTable.playOnPile(teamId, card);
        myTable.setSafetyArea(teamId,safetyArea);
        myTable.setHazardStatus(teamId,hazardStatus);
        handScorer.setMilesTraveled(teamId,handMileage);

    }// end playOnPile

    //############################################
    Player getPlayer(int index)
    {
        return playerVect.get(index);
    } // end getPlayer

    //############################################
    int getTwoHundredsLeft()
    {
        Table myTable = Table.getInstanceOf();
        return myTable.twoHundredsLeft[teamId];
    } // end getPlayer

    //############################################
    void addMiles(int miles)
    {
        //adds miles if less than equal to 1000
        if(handMileage+miles<=1000)
        {
            handMileage+=miles;
            //gameScore+=miles;
        }//end if
        //ignores and discard if greater than trip of 1000
    } // end getPlayer

    //############################################
    Player getActivePlayer()
    {
        return playerVect.get(activePlayer);
    } // end getPlayer

    //############################################
    void toggleActivePlayer()
    {
        activePlayer = (activePlayer+1)%2;
    }//end toggleACtivePlayer


    //############################################
    Card feedMe()
    {//might not need
        Card newCard = new Card();
        return newCard;
    }//end feedme

    //############################################
    void giveCard(Card newCard)
    {
        getActivePlayer().addCard(newCard);
    }//end giveCard

    //############################################
    String getSafety(int index)
    {
        Table myTable = Table.getInstanceOf();
        Boolean[] safetyArea = myTable.getSafetyArea(teamId);
        Boolean[] hazardStatus = myTable.getHazardStatus(teamId);
        
        if(safetyArea[index])
        return "YES";
        else
        return "NO";
    }//end giveCard

        //############################################
    String getSpeedLimit()
    {
        Table myTable = Table.getInstanceOf();
        Boolean[] safetyArea = myTable.getSafetyArea(teamId);
        Boolean[] hazardStatus = myTable.getHazardStatus(teamId);

        if(hazardStatus[1])
        return "25/50";
        else
        return "NONE";
    }//end giveCard

    //############################################
    boolean checkValidDistance(int miles)
    { //order: stop, speedLimit, out of gas, flat-tire, accident

        Table myTable = Table.getInstanceOf();
        Boolean[] safetyArea = myTable.getSafetyArea(teamId);
        Boolean[] hazardStatus = myTable.getHazardStatus(teamId);

        if(hazardStatus[0]==false||safetyArea[0]==true)
        {//we have roll or right of way, check for other hazards

            if(hazardStatus[1]==true)
            {//speed limite active
                if(miles>50)
                    return false;
            }//end if limit

             if(hazardStatus[2]==true||hazardStatus[3]==true||hazardStatus[4]==true)
            {//hazards are active
                return false;
            }//end if

             else//no hazard+roll
             {
                if(miles!=200)
                {
                    return true;
                }
                else//we have a 200
                {
                    if(myTable.twoHundredsLeft[teamId]>0)
                    {//we have not played too many two hundreds                    
                        return true;
                    }
                    else
                        return false;
                }//end else
             }//end else
        }//end if
        else//no roll or row card
        {return false;}//end else
    }//end giveCard

    
    //#########################################################33
    Card getBPCard()
    {
        Card myBP = Table.getInstanceOf().teamPiles[teamId].get(1).topCard();
        return myBP;
    }//end getBPCard

    
  //#########################################################33
    Card getSPCard()
    {
        Card mySP = Table.getInstanceOf().teamPiles[teamId].get(0).topCard();
        return mySP;

    }//end getSPCard

    
//#########################################################33
    void resetTeam()
    {
     Table myTable = Table.getInstanceOf();
     handMileage=0;
     myTable.resetHazards(teamId);
     myTable.resetSafety(teamId);
     myTable.twoHundredsLeft[teamId] = 2;
    }
//#########################################################33
     boolean checkValidHazard(int ID)
    { //order: stop, speedLimit, out of gas, flat-tire, accident
        Table myTable = Table.getInstanceOf();
        Boolean[] safetyArea = myTable.getSafetyArea(teamId);
        
        boolean valid = false;
         switch(ID)
            {
             case 6://out of gas
                if(safetyArea[3]==false)//et
                {
                  valid = true;
                }//end if
                    break;
            case 7://flat tire
                if(safetyArea[2]==false)//pp
                {
                  valid = true;
                }//end if
                    break;
            case 8://accident
                if(safetyArea[1]==false)//da
                {
                  valid = true;
                }//end if
                    break;
            case 9://speedlmit
                if(safetyArea[0]==false)//row
                {
                  valid = true;
                }//end if
                    break;
            case 10://stop
                if(safetyArea[0]==false)//row
                {
                  valid = true;
                }//end if
                    break;
        }//end switch
        return valid;

    }//end validhazard

//#########################################################33
     boolean checkValidRemedy(int ID)
    { //hazard order: stop, speedLimit, out of gas, flat-tire, accident

        Table myTable = Table.getInstanceOf();
        Boolean[] safetyArea = myTable.getSafetyArea(teamId);
        Boolean[] hazardStatus = myTable.getHazardStatus(teamId);

        boolean valid = false;
         switch(ID)
        {
            case 11:
                if(hazardStatus[2]==true)//gas
                {
                  valid = true;
                }//end if
                 break;
            case 12:
                if(hazardStatus[3]==true)//tire
                {
                  valid = true;
                }//end if
                  break;
             case 13:
                 if(hazardStatus[4]==true)//accident
                {
                  valid = true;
                }//end if
                  break;
             case 15://roll card, no hazards can be present, except limit:1
                 if(hazardStatus[0]==true
                     &&safetyArea[0]==false
                     &&hazardStatus[2]==false
                     &&hazardStatus[3]==false
                     &&hazardStatus[4]==false)
                {
                  valid = true;
                }//end if
                    break;
            case 14: 
                if(hazardStatus[1]==true)//limit
                {
                  valid = true;
                }//end if
                    break;
        }//end switch
        return valid;

    }//end validremedy

    //############################################
    void giveHands(Vector<Card> teamHands)
    {//takes 12 card vector to distribute to players' hands
        playerVect.get(0).getHand().clearHand();
        playerVect.get(1).getHand().clearHand();
        for( int i = 0 ; i < teamHands.size()/2 ; i++ )
        {
            playerVect.elementAt(0).addCard(teamHands.elementAt(i));
        }
        for( int j = teamHands.size()/2 ; j < teamHands.size() ; j++ )
        {
            playerVect.elementAt(1).addCard(teamHands.elementAt(j));
        }

    }//end giveCard

    //############################################
    @Override
    public String toString()
    {
        return "Team Name: " + name + "\n" +
               "------------------------------" + "\n" +
               playerVect.elementAt(0).toString() + "\n" +
               playerVect.elementAt(1).toString();
    }
}//end class Team
